package com.yxh.controller;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.Change;
import com.yxh.pojo.FlowShip;
import com.yxh.pojo.History;
import com.yxh.pojo.vo.UserRestPasswordVO;
import com.yxh.service.FlowNodeService;
import com.yxh.util.APIErrorCode;
import com.yxh.util.Response;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.TaskCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by WL on 2018/7/23.
 */

@Scope("prototype")
@RestController
@RequestMapping(value = "/flowNode")
public class FlowNodeController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(FlowNodeController.class);
    private Response response;
    @Autowired
    private FlowNodeService flowNodeService;

    /**
     *新增 流程 节点中 默认指定人
     *
     * @param flowShip
     * @return
     */
    @PostMapping
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveChange(@RequestBody FlowShip flowShip) {
        int i;
        String id= SnowflakeIdWorker.creatId();
        List<Map<String,Object>> flow=  flowNodeService.getLists(flowShip);
        if(flow.size()==0){

            flowShip.setId(id);
             i=flowNodeService.save(flowShip);
        }
        else{
            flowShip.setId(flow.get(0).get("id").toString());
            i=flowNodeService.update(flowShip);
        }

        if(i==1){
            response = Response.returnResponse(APIErrorCode.SUCCESS);
            response.setData(id);
        }
        else
            response = Response.returnResponse(APIErrorCode.INTERNAL_ERROR);


        return response;
    }


}
